package com.codegzy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codegzy.dao.UserDao;
import com.codegzy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public int save(User user) {
        User userSelect = findByUsername(user.getUsername());
        if (!ObjectUtils.isEmpty(userSelect)) {
            throw new RuntimeException("用户名已存在");
        }
        return userDao.insert(user);
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userDao.selectOne(queryWrapper);
    }

    @Override
    public User findByUsernameAndPassword(String username, String password) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        userQueryWrapper.eq("password", password);
        return userDao.selectOne(userQueryWrapper);
    }
}
